package com.hd.determine.modules.alarm.determine.mediator;

import com.alibaba.fastjson.JSON;
import com.hd.common.constant.BizConstant;
import com.hd.common.utils.DateUtil;
import com.hd.common.utils.GenUtil;
import com.hd.determine.config.redis.prefix.CacheKey;
import com.hd.determine.modules.alarm.Determiner;
import com.hd.determine.modules.alarm.model.AlarmRule;
import com.hd.determine.modules.alarm.service.IAlarmDetailService;
import com.hd.determine.modules.alarm.util.AlarmUtils;
import com.hd.determine.modules.vehicle.model.VehicleEndPos;
import com.hd.determine.modules.vehicle.utils.VehicleUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title DetermineMediator.java
 * @Description 研判中介
 * @Package com.hd.sup.modules.alarm.determine.mediator
 * @date 2021/1/29 11:41 上午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
@Component
public class DetermineMediator {

    @Resource
    private IAlarmDetailService alarmDetailService;


    @Resource/**/
    private VehicleUtils vehicleUtils;

    /**
     * @return void
     * @author chentl
     * @description 指挥研判
     * @date 2021/1/29 11:43 上午
     * @params [message]
     */
//    @Async("superviseTaskExecutor")
    public void prepareDetermine(String message) {
        VehicleEndPos data = JSON.parseObject(message, VehicleEndPos.class);

        String transType = data.getTransType();
        boolean scheduled = data.isScheduled();

        if (StringUtils.isBlank(transType)) return;

        //次日消息抛弃
        if (AlarmUtils.isOverDay(DateUtil.formatDate(data.getGpsTime(), BizConstant.Common.DATE_FORMAT_SECOND.key())))
            return;

        //遍历支持的报警类型  支持后续扩展成动态配置报警类型
        for (BizConstant.AlarmType alarmType : BizConstant.AlarmType.values()) {

            if (BizConstant.AlarmType.OVER_SPEED.key().equals(alarmType.key())) continue;
            //查缓存取规则
            AlarmRule rule = vehicleUtils.currentVehicleRuleCache(GenUtil.cacheKey(CacheKey.TRANSTYPE_RULE_KEY, alarmType.val()), transType);

            if (rule == null) continue;

            //开始研判
            boolean needMonitor = AlarmUtils.needMonitor(rule.getStartTime(), rule.getEndTime(), data.getGpsTime());

            if (!needMonitor) continue;

            Determiner<VehicleEndPos> determiner = Determiner.getDeterminer(alarmType, data, rule, scheduled);

            if (determiner == null) continue;

            determiner.startDetermine();

            // 后置操作
            // 产生报警
            if (determiner.needProduce) alarmDetailService.generateAlarmData(determiner.data, rule);

            // 停止报警
            if (determiner.needStop) alarmDetailService.stopAlarm(determiner.data, alarmType);

        }


    }

}
